/**
 * 
 */
package com.lanswon.qzsmk.util;

import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.exception.ErrorCode;
import com.lanswon.qzsmk.model.Certificate;
import com.lanswon.qzsmk.model.Nation;

/**
 * 验证数据格式工具类
 * 
 * @author sun
 *
 */
public class ValidateDataUtil {
	public static int MAX_LENGTH_10 = 10;
	public static int MAX_LENGTH_15 = 15;
	public static int MAX_LENGTH_20 = 20;
	public static int MAX_LENGTH_50 = 50;
	
	/**
	 * 验证证件类型名称
	 * @param paperName
	 * @param list
	 * @throws ApplicationException
	 */
	public static void checkPaperName(String paperName,List<Certificate> list) throws ApplicationException{
			boolean b =false;
		for(Certificate c:list){
				if(paperName.equals(c.getCertificatesName())){
					b =true;
					break;
				}
			}
		
		if(!b){
			throw new ApplicationException(ErrorCode.OTHER_EXCEPTION, "证件类型填写错误");

		}
	}
	
	/**
	 * 验证民族名称
	 * @param paperName
	 * @param list
	 * @throws ApplicationException
	 */
	public static void checkNationName(String NationName,List<Nation> list) throws ApplicationException{
			boolean b =false;
		for(Nation c:list){
				if(NationName.equals(c.getNationname())){
					b =true;
					break;
				}
			}
		
		if(!b){
			throw new ApplicationException(ErrorCode.OTHER_EXCEPTION, "民族【"+NationName+"】填写错误");

		}
	}
	
	/**
	 * 验证长度
	 * @param str
	 * @param maxlength
	 * @throws ApplicationException 
	 */
	public static void checklength(String str, int maxlength) throws ApplicationException {
		if (StringUtils.isNotEmpty(str) && str.length() > maxlength)
			throw new ApplicationException(ErrorCode.OTHER_EXCEPTION, "【"+str+"】长度已超出最大限制【"+maxlength+"】个字符");
	}

	
	
	/**
	 * 验证邮编
	 * @param zipCode
	 * @return
	 */
	public static boolean isZipCode(String zipCode) {
		String regex_zipCode = "^[0-9]{6}$";
		return Pattern.matches(regex_zipCode, zipCode.trim());
	}
	
	@Test
	public void testIsZipCode(){
		System.out.println(isZipCode("222111"));
	}

	/**
	 * 验证手机号
	 * 
	 * @param mobile
	 * @return
	 */
	public static boolean isMobile(String mobile) {
//		String regex_mobile = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
		String regex_mobile = "^1(3|4|5|7|8)\\d{9}$";
		return Pattern.matches(regex_mobile, mobile);
	}

	/**
	 * 验证身份证号
	 * 
	 * @param mobile
	 * @return
	 */
	public static boolean isIDCard(String cardNo) {
		String regex_id_card = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";
		return Pattern.matches(regex_id_card, cardNo);
	}

	public static void main(String[] args) {
		System.out.println(isIDCard("32072119910815521x"));

	}

	/**
	 * 验证中文姓名
	 * 
	 * @param mobile
	 * @return
	 */
	public static boolean isChineseName(String customerName) {
		String regex_chinese_name = "(^[\u4e00-\u9fa5]{2,8}$)";
		return Pattern.matches(regex_chinese_name, customerName);
	}

	
	
	/**
	 * 验证英文+数字
	 * 
	 * @param mobile
	 * @return
	 */
	public static boolean isEnglishAndNumName(String eName) {
		String regex_english_name = "(^[A-Za-z0-9]+$)";
		return Pattern.matches(regex_english_name, eName);
	}
	
	
	/**
	 * 验证英文姓名
	 * 
	 * @param mobile
	 * @return
	 */
	public static boolean isEnglishName(String eName) {
		String regex_english_name = "([a-zA-Z]{3,10})";
		return Pattern.matches(regex_english_name, eName);
	}

	/**
	 * 验证电子邮箱
	 * 
	 * @param mobile
	 * @return
	 */
	public static boolean isEmail(String email) {
		String regex_email = "(^([a-zA-Z0-9_-]|[\\.]|[\\u4E00-\\u9FA5]|[\\uFE30-\\uFFA0])+@([a-zA-Z0-9_-])+((\\.[a-zA-Z0-9_-]{2,3}){1,2})$)";
		return Pattern.matches(regex_email, email);
	}
	
	
	@Test
	public void testlength(){
		String s ="我爱你";
				
				System.out.println(s.length());;
	}

}
